package com.zmzncs.lmtc.module.order.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmzncs.lmtc.common.pojo.LoginUser;
import com.zmzncs.lmtc.common.pojo.enums.optionalvalue.order.OrderRefundStatusEnum;
import com.zmzncs.lmtc.common.util.JwtUtil;
import com.zmzncs.lmtc.module.customer.dao.CardRecordDao;
import com.zmzncs.lmtc.module.customer.pojo.card_record.CardRecordDTO;
import com.zmzncs.lmtc.module.order.dao.OrderInfoDao;
import com.zmzncs.lmtc.module.order.dao.OrderRefundMapper;
import com.zmzncs.lmtc.module.order.pojo.order_info.OrderInfoDTO;
import com.zmzncs.lmtc.module.order.pojo.order_refund.OrderRefund;
import com.zmzncs.lmtc.module.order.pojo.order_refund.OrderRefundApplyForm;
import com.zmzncs.lmtc.module.order.pojo.order_refund.OrderRefundDTO;
import com.zmzncs.lmtc.module.order.service.OrderRefundService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 订单-退款 服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2020-10-21
 */
@Service
public class OrderRefundServiceImpl extends ServiceImpl<OrderRefundMapper, OrderRefund> implements OrderRefundService {

    @Autowired
    OrderRefundMapper orderRefundMapper;
    @Autowired
    OrderInfoDao orderInfoDao;
    @Autowired
    CardRecordDao cardRecordDao;

    @Autowired
    HttpServletRequest request;

    /**
     * 申请退款
     */
    @Override
    @Transactional
    public Map<String,Object> applyRefund(@Valid OrderRefundApplyForm form) {
        Map<String,Object> map = new HashMap<>();

        //根据订单号查询订单
        OrderInfoDTO orderInfoDTO = orderInfoDao.getByOrderCode(form.getOrderCode());
        if (orderInfoDTO == null){
            map.put("status", false);
            map.put("msg","订单号错误，未查询到订单");
            return map;
        }
        Date inTime = orderInfoDTO.getInTime();
        Date outTime = orderInfoDTO.getOutTime();
        outTime = outTime == null ? inTime : outTime;
        Long parkId = orderInfoDTO.getParkId();
        String carNumber = orderInfoDTO.getCarNumber();
        //查询用户月卡信息
        CardRecordDTO cardParkDTO = cardRecordDao.getCustomerMonthCardRecord(parkId, carNumber, outTime);
        if (cardParkDTO == null){
            map.put("status", false);
            map.put("msg",carNumber + "没有月卡，无法退款");
            return map;
        }

        OrderRefund orderRefund = new OrderRefund();
        orderRefund.setOrderCode(form.getOrderCode()); //进出场订单号
        orderRefund.setCardRecordOrderCode(cardParkDTO.getOrderCode()); //购买会员卡订单号
        orderRefund.setReason(form.getReason());    //  退款原因
        orderRefund.setRefundAmount(orderInfoDTO.getOutPayMoney());
        LoginUser loginUser = JwtUtil.getLoginUser(request);
        orderRefund.setCustomerId(loginUser.getCustomerId());    //  客户id
        StringBuffer imgUrlBuffer = new StringBuffer();
        for(String imgUrl: form.getImgUrlList()){
            imgUrlBuffer.append(imgUrl).append(",");
        }
        orderRefund.setImgUrl(imgUrlBuffer.deleteCharAt(imgUrlBuffer.length()-1).toString());   //  图片地址，多个图片以英文逗号隔开
        orderRefund.setStatus(OrderRefundStatusEnum.TO_BE_AUDIT.getCode()); //  状态：（0：待审核，1：审核中，2：通过，3：拒绝）

        orderRefundMapper.insert(orderRefund);

        map.put("status", true);
        map.put("msg","申请退款提交成功");
        return map;
    }

    /**
     * 查询退款信息
     */
    @Override
    public IPage<OrderRefundDTO> getOrderRefundPage(Page page) {
        LoginUser loginUser = JwtUtil.getLoginUser(request);
        IPage<OrderRefundDTO> orderRefundPage = orderRefundMapper.getOrderRefundPage(page, loginUser.getCustomerId());
        return orderRefundPage;
    }

}
